package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.MateriaDAO;
import entity.Materia;

@WebServlet(name = "MateriaController", urlPatterns = { "/materiaInsert",
		"/materiaUpdate", "/materiaDelete", "/materiaList" })
public class MateriaController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public MateriaController() {

	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		List<Materia> materia = null;

		switch (action) {

		case "confirmDelete":
			String par = request.getParameter("id");
			request.setAttribute("id", par);
			request.getRequestDispatcher(
					"/view/private/materia/confirmDelete.jsp").forward(request,
					response);
			break;

		case "list":

			materiaList(request, response);

			break;

		case "delete":

			if (MateriaDAO.delete(new Integer(request.getParameter("id")))) {
				response.sendRedirect("" + request.getContextPath()
						+ "/materiaList?action=list");
			} else {
				response.sendRedirect("" + request.getContextPath()
						+ "/materiaList?action=list");
			}
			break;

		case "altera":

			materia = MateriaDAO.findById(new Integer(request
					.getParameter("id")));
			request.setAttribute("lista", materia);
			request.getRequestDispatcher("/view/private/materia/altera.jsp")
					.forward(request, response);

			break;
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		Materia materia = new Materia();

		switch (action) {

		case "update":

			materia.setId(new Integer(request.getParameter("id")));
			materia.setNome(request.getParameter("nome"));
			materia.setDescricao(request.getParameter("descricao"));

			if (MateriaDAO.update(materia)) {
				// request.setAttribute("msg", "Materia Alterada com Sucesso!");
				response.sendRedirect("" + request.getContextPath()
						+ "/materiaList?action=list");
			} else {
				response.sendRedirect("" + request.getContextPath()
						+ "/materiaList?action=list");

			}

			break;

		case "insert":

			materia.setNome(request.getParameter("nome"));
			materia.setDescricao(request.getParameter("descricao"));

			if (MateriaDAO.insert(materia)) {
				response.sendRedirect("" + request.getContextPath()
						+ "/materiaList?action=list");
			} else {
				response.sendRedirect("" + request.getContextPath()
						+ "/msg_erro.jsp");
			}
			break;
		}
	}

	private void materiaList(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Materia> materias = MateriaDAO.getAll();
		request.setAttribute("lista", materias);
		
		Integer totalLogados = (Integer) getServletContext().getAttribute(
				"qde_Users");
		request.setAttribute("totalLogados", totalLogados);
		
		request.getRequestDispatcher("/view/private/materia/lista.jsp")
				.forward(request, response);

	}
}
